//
//  ShouCangJiaViewController.m
//  一键联IOS
//
//  Created by 徐绍明 on 15/12/25.
//  Copyright © 2015年 xufei. All rights reserved.
//

#import "ShouCangJiaViewController.h"
#import "shoppingTableViewCell.h"
#import "LoginVC.h"
@interface ShouCangJiaViewController ()

@end

@implementation ShouCangJiaViewController
- (IBAction)backBtn:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)editBtn:(id)sender {
    UIButton *btn = (UIButton *)sender;
    btn.selected = !btn.selected;
    if (btn.selected) {
        _isEdit = YES;
        [btn setTitle:@"完成" forState:UIControlStateSelected];
    }else{
        _isEdit = NO;
        [btn setTitle:@"编辑" forState:UIControlStateNormal];
    }
    [_shouCangTableView reloadData];
    
}
-(void)viewWillAppear:(BOOL)animated{
    [HttpsManager shouCangJiaType:@"2" page:@"0" completionBlock:^(NSMutableArray *array) {
              if (array == nil) {
            LoginVC *log = [[LoginVC alloc] init];
            [self.navigationController pushViewController:log  animated:YES];
        }else{
            _array = array;
            for (int i = 0;  i<array.count; i++) {
                [_shouCangDict setObject:@"NO" forKey:[NSString stringWithFormat:@"%d",i]];
                [_arr addObject:@"YES"];
                
            }

            if (array.count ==0) {
                SHOW_ALERT(@"收藏夹无商品！");
            }
            
            
        }
        
     [_shouCangTableView reloadData];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _shouCangDict = [NSMutableDictionary dictionary];
     _arr = [NSMutableArray array];
  
    _isEdit = NO;
    _shouCangTableView.tableFooterView = [[UIView alloc] init];
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(WIDE/2-60, 27, 120, 30  )];
    title.text = @"收藏夹";
    title.textAlignment = NSTextAlignmentCenter;
    title.textColor = [UIColor whiteColor];
    [self.view addSubview:title];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _array.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    shoppingTableViewCell *cell = nil;
    
    if (_isEdit == NO) {
        if (!cell) {
            cell = [tableView dequeueReusableCellWithIdentifier:@"shoucang1"];
            cell = [[NSBundle mainBundle] loadNibNamed:@"shoppingTableViewCell" owner:nil options:nil][4];
        }
        ShouCangJia *shoucang = _array[indexPath.row];
        [cell.shouCang1ImgUrl sd_setImageWithURL:[NSURL URLWithString:shoucang.goodImgUrl] placeholderImage:[UIImage imageNamed:@"默认图片.jpg"]];
        cell.shouCang1Name.text = shoucang.goodName;
        cell.shouCang1DIsPrice.text = [NSString stringWithFormat:@"%.2f",shoucang.goodDisPrice.intValue *0.01];
        NSDictionary *dic = @{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]};
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%.2f",shoucang.goodOldPrice.intValue *0.01] attributes:dic];
        
        cell.shouCang1OldPrice.attributedText = attStr;

    }else{
        if (!cell) {
            cell = [tableView dequeueReusableCellWithIdentifier:@"shoucang2"];
            cell = [[NSBundle mainBundle] loadNibNamed:@"shoppingTableViewCell" owner:nil options:nil][5];
        }
        ShouCangJia *shoucang = _array[indexPath.row];
      
        [cell.shouCang2ImgUrl sd_setImageWithURL:[NSURL URLWithString:shoucang.goodImgUrl] placeholderImage:[UIImage imageNamed:@"默认图片.jpg"]];
         cell.shouCnag2Name.text = shoucang.goodName;
        cell.shouCang2DisPrice.text = [NSString stringWithFormat:@"%.2f",shoucang.goodDisPrice.intValue *0.01];
        NSDictionary *dic = @{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]};
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%.2f",shoucang.goodOldPrice.intValue *0.01] attributes:dic];
        
        cell.shouCang2OldPrice.attributedText = attStr;
        cell.shouCang2Btn.tag = indexPath.row;
        if ([[_shouCangDict objectForKey:[NSString stringWithFormat:@"%d",(int)indexPath.row]] isEqualToString:@"YES"]) {
            [cell.shouCang2Btn setImage:[UIImage imageNamed:@"购物车选中"] forState:UIControlStateNormal];
        }else{
            [cell.shouCang2Btn setImage:[UIImage imageNamed:@"购物车未选择"] forState:UIControlStateNormal];

        }
        [cell.shouCang2Btn addTarget:self action:@selector(shouCangEditBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        UIView *lowView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT-40, WIDE, 40)];
        [self.view addSubview:lowView];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
         if ([_shouCangDict.allValues isEqualToArray:_arr]) {
           
             [btn setImage:[UIImage imageNamed:@"购物车选中"] forState:UIControlStateNormal];
        }else{
       
             [btn setImage:[UIImage imageNamed:@"购物车未选择"] forState:UIControlStateNormal];
        }
        NSLog(@"view %@ //////%@+++++%@",_shouCangDict,_shouCangDict.allValues,_arr);
 
        btn.frame = CGRectMake(10, 5, 30, 30);
        [btn addTarget:self action:@selector(selectAllBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        [lowView addSubview:btn];
        UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(45, 5, 100, 30)];
        lb.text = @"全选";
        [lowView addSubview:lb];
        
        UIButton *removeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [removeBtn setTitle:@"删除收藏" forState:UIControlStateNormal];
        removeBtn.frame = CGRectMake(WIDE-100  , 0, 100, 40);
        removeBtn.backgroundColor = RGB_COLOR(0x00c49f);
        [removeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [removeBtn addTarget:self action:@selector(removeGoodsBtn) forControlEvents:UIControlEventTouchUpInside];
        [lowView addSubview:removeBtn];
        
        
        
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    
    return cell;
}
-(void)selectAllBtn:(UIButton *)btn{
    
    
    if ([_shouCangDict.allValues isEqualToArray:_arr]) {
         [btn setImage:[UIImage imageNamed:@"购物车未选择"] forState:UIControlStateNormal];
        for (int x = 0; x< _shouCangDict.allKeys.count; x++) {
            [_shouCangDict setObject:@"NO" forKey:[NSString stringWithFormat:@"%d",x]];
        }
    
    }else{
        for (int y = 0; y< _shouCangDict.allKeys.count; y++) {
            [_shouCangDict setObject:@"YES" forKey:[NSString stringWithFormat:@"%d",y]];
        }
    }
    
    [_shouCangTableView reloadData];
 
   
}
-(void)removeGoodsBtn{
   
    for (int i = 0 ; i<_arr.count; i++) {
        
        
        if ([[_shouCangDict objectForKey:[NSString stringWithFormat:@"%d",i]] isEqualToString:@"YES"]) {
            ShouCangJia *shou = _array[i];
             [GMDCircleLoader setOnView:self.view withTitle:nil animated:YES];
            [HttpsManager removeShouCangWithType:@"2" favorited:shou.goodShopID completionBlock:^(NSString *errmsg) {
                SHOW_ALERT(errmsg);
            
               [_shouCangDict removeObjectForKey:[NSString stringWithFormat:@"%d",i]];
                [_arr removeObjectAtIndex:i];
                [_array removeObjectAtIndex:i];
                [_shouCangTableView reloadData];

                [GMDCircleLoader hideFromView:self.view animated:YES];
           }];

        }
    }
    NSLog(@"remove %@ //////// %@ //////%@",_shouCangDict,_arr,_array);
   
}

-(void)shouCangEditBtn:(UIButton *)btn{
  
    if ([[_shouCangDict objectForKey:[NSString stringWithFormat:@"%d",(int)btn.tag]] isEqualToString:@"NO"]) {
       // [btn setImage:[UIImage imageNamed:@"购物车选中"] forState:UIControlStateSelected];
         [_shouCangDict setObject:@"YES" forKey:[NSString stringWithFormat:@"%d",(int)btn.tag]];
    }else{
    
        [_shouCangDict setObject:@"NO" forKey:[NSString stringWithFormat:@"%d",(int)btn.tag]];
        _allBtn.selected = NO;
       
    }
    
   
    [_shouCangTableView reloadData];

    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
